package com.flyqiu.form.core.event;

import com.alibaba.fastjson2.JSONObject;
import com.flyqiu.common.mybaits.plus.FlyQiuQueryWrapper;
import com.flyqiu.form.api.event.FlyQiuBaseFormEventManage;
import com.flyqiu.form.api.event.FlyQiuTableDataEventManage;
import com.flyqiu.form.api.event.FlyQiuTableDataEvent;
import com.flyqiu.form.model.entity.FlyQiuTableConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.List;


/**
 * 表数据分发器
 */
@Slf4j
public class DefaultFlyQiuDataEventManage extends FlyQiuBaseFormEventManage<DefaultFlyQiuDataEventManage, FlyQiuTableDataEvent> implements FlyQiuTableDataEventManage {
    public DefaultFlyQiuDataEventManage() {

    }

    @Override
    public void register(FlyQiuTableDataEvent event) {
        addEvent(event);
    }

    @Override
    public void unregister(FlyQiuTableDataEvent event) {
        removeEvent(event);
    }


    /**
     * 保存数据前
     *
     * @param formConfig 数据配置
     * @param data       数据
     */
    @Override
    public void beforeSaveData(FlyQiuTableConfig formConfig, JSONObject data) {

        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeSaveData(formConfig, data);
            }
        }
    }

    /**
     * 保存数据后
     *
     * @param formConfig 表配置
     * @param data       保存的数据
     */
    @Override
    public void afterSaveData(FlyQiuTableConfig formConfig, JSONObject data) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterSaveData(formConfig, data);
            }
        }
    }

    /**
     * 修改数据前
     *
     * @param formConfig 表配置
     * @param data       数据
     * @param wrapper
     */
    @Override
    public void beforeUpdate(FlyQiuTableConfig formConfig, JSONObject data, FlyQiuQueryWrapper wrapper) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeUpdate(formConfig, data, wrapper);
            }
        }
    }

    /**
     * 修改数据后
     *
     * @param formConfig 表配置
     * @param data       数据
     * @param wrapper    条件
     */
    @Override
    public void afterUpdate(FlyQiuTableConfig formConfig, JSONObject data, FlyQiuQueryWrapper wrapper) {

        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterUpdate(formConfig, data, wrapper);
            }
        }
    }

    /**
     * 删除数据前
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     */
    @Override
    public void beforeDelete(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeDelete(formConfig, wrapper);
            }
        }
    }

    /**
     * 删除数据后
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     */
    @Override
    public void afterDelete(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterDelete(formConfig, wrapper);
            }
        }
    }

    /**
     * 数据查询前
     *
     * @param formConfig 数据表配置
     * @param wrapper    条件
     */
    @Override
    public void beforeQueryData(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper) {

        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeQueryData(formConfig, wrapper);
            }
        }
    }

    /**
     * 数据查询后
     *
     * @param formConfig 表配置
     * @param wrapper    条件
     * @param records    数据
     */
    @Override
    public void afterQueryData(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper, JSONObject... records) {

        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterQueryData(formConfig, wrapper, records);
            }
        }
    }

    /**
     * 查询数量之前
     *
     * @param formConfig 数据表配置
     * @param wrapper    查询条件
     */
    @Override
    public void beforeCount(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.beforeCount(formConfig, wrapper);
            }
        }
    }

    /**
     * 查询数量之后
     *
     * @param formConfig 数据表配置
     * @param wrapper    查询条件
     */
    @Override
    public void afterCount(FlyQiuTableConfig formConfig, FlyQiuQueryWrapper wrapper) {
        List<FlyQiuTableDataEvent> listeners = getEvents(UNION_KEY_PREFIX, formConfig.getTableName());
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
        listeners = null;
        if (StringUtils.isNotEmpty(formConfig.getFormCategory())) {
            listeners = getEvents(GROUP_PREFIX, formConfig.getFormCategory());
        }
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
        listeners = getEvents(GLOBAL_PREFIX, GLOBAL_KEY);
        if (listeners != null && !listeners.isEmpty()) {
            for (FlyQiuTableDataEvent listener : listeners) {
                listener.afterCount(formConfig, wrapper);
            }
        }
    }


}
